﻿using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Easy.Announcement
{
    public partial class EasyAnnIsotopeUserControl : UserControl
    {
        public EasyAnnIsotopeWebPart.EasyAnnIsotopeWebPart WebPart { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            SPList list = SPContext.Current.Web.Lists.TryGetList(this.ListName);
            if (list != null)
            {
                try
                {
                    SPView view = list.Views[this.ViewName];
                    
                    if (view != null)
                    {
                        this.Items = list.GetItems(new SPQuery() { Query = view.Query });
                        if (!string.IsNullOrEmpty(FilterByField))
                        {
                            SPField field = list.Fields.TryGetFieldByStaticName(this.FilterByField);
                            if (field != null)
                            {
                                if (field is SPFieldChoice)
                                {
                                    SPFieldChoice choiceField = field as SPFieldChoice;
                                    FilterByChoices = new string[choiceField.Choices.Count];
                                    choiceField.Choices.CopyTo(FilterByChoices, 0);
                                }
                                else
                                {
                                    e_setErrorMsg(string.Format("Field \"{0}\" is not a CHOICE type. Please select one that is CHOICE Type for Filter.", this.FilterByField));
                                }
                            }
                            else
                            {
                                e_setErrorMsg(string.Format("Unable to locate \"{0}\" field of \"{1}\" List in current web.", this.FilterByField, this.ListName));
                            }
                        }

                        if (!string.IsNullOrEmpty(SortByJSONField))
                        {

                            SortByChoices = SortByJSONField.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

                        }
                        else
                        {
                            SortByChoices = new string[] { };
                        }
                    }
                }
                catch (Exception ex)
                {
                    e_setErrorMsg(string.Format("Unable to render \"{0}\" view of \"{1}\" List in current web. <br/>{2}", this.ViewName, this.ListName, ex.StackTrace));
                }

            }
            else
            {
                e_setErrorMsg(string.Format("Unable to locate \"{0}\" list in current web.", this.ListName));
            }
        }

        public string GetCSSClassName(string name)
        {
            return name.Replace(' ', '_').ToLower();
        }
        protected string ListName { get { return WebPart.ListName; } }
        protected string TitleField { get { return WebPart.TitleField; } }
        protected string PublishDateField { get { return WebPart.PublishDateField; } }
        protected string SummaryField { get { return WebPart.SummaryField; } }
        protected string ViewName { get { return WebPart.ViewName; } }
        protected string FilterByField { get { return WebPart.FilterByField; } }
        protected string[] FilterByChoices { get; set; }
        protected string SortByJSONField { get { return WebPart.SortByFields; } }
        protected string[] SortByChoices { get; set; }
        protected string GetSortByDataJSON()
        {
           Dictionary<string,string> sortData = new Dictionary<string,string>();
            if(SortByChoices !=null && SortByChoices.Length > 0)
            {
                foreach(string sortBy in SortByChoices)
                {
                    var val = sortBy.ToLower();
                    sortData.Add(val, "." + val);
                }
            }

            return new JavaScriptSerializer().Serialize(sortData);
        }
        protected SPListItemCollection Items { get; set; }

        private void e_setErrorMsg(string text)
        {
            errorMsg.InnerHtml = text;
            errorMsg.Visible = !string.IsNullOrEmpty(text);
        }
    }
}
